package com.shark.teach;

import java.util.ArrayList;

/**
 * JDK8使用
 * -Xms30m -Xmx30m -XX:+PrintGCDetails
 *
 * JDK17使用，-XX:+PrintGCDetails参数在JDK17版本中不再被推荐
 * [warning][gc] -XX:+PrintGCDetails is deprecated. Will use -Xlog:gc* instead.
 * -Xms30m -Xmx30m -Xlog:gc*
 *
 *
 * -Xms20m -Xmx20m -Xlog:gc* -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\myDump
 *
 *
 * @auther shark
 * @create 2024-07-27 16:50
 */
public class OOMDemo
{
    public static void main(String[] args) throws InterruptedException {
        ArrayList<Dog> list = new ArrayList<>();
        while(true){

            System.out.print("最大堆大小：Xmx=");
            System.out.println(Runtime.getRuntime().maxMemory() / 1024.0 / 1024 + "M");

            System.out.print("剩余堆大小：free mem=");
            System.out.println(Runtime.getRuntime().freeMemory() / 1024.0 / 1024 + "M");

            System.out.print("当前堆大小：total mem=");
            System.out.println(Runtime.getRuntime().totalMemory() / 1024.0 / 1024 + "M");

            list.add(new Dog());

            Thread.sleep(100);
            System.out.println();

            System.gc();   // C  C++  java = (C++)--
        }
    }
}

//-Xms30m -Xmx30m -XX:+PrintGCDetails  K:V

//-Xms20m -Xmx20m -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\myDump

class Dog
{
    Integer id;

    String  dogName;

    byte[] bytes = new byte[1*1024*1024];//1MB
}